﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Game.model;
using Game.utils;
using java.lang;
using java.util;
using lineage2.commons.threading;
using String = System.String;

namespace Game.ai
{
    public class CharacterAI:AbstractAI
    {
        public CharacterAI(Creature actor):base(actor)
	    {
		    //super(actor);
	    }
	    
	    protected override void onIntentionIdle()
	    {
		    clientStopMoving();
		    changeIntention(CtrlIntention.AI_INTENTION_IDLE, null, null);
	    }
	    
	    protected override void onIntentionActive()
	    {
		    clientStopMoving();
		    changeIntention(CtrlIntention.AI_INTENTION_ACTIVE, null, null);
		    onEvtThink();
	    }
	    
	    protected override void onIntentionAttack(Creature target)
	    {
		    setAttackTarget(target);
		    clientStopMoving();
		    changeIntention(CtrlIntention.AI_INTENTION_ATTACK, target, null);
		    onEvtThink();
	    }
        
	    protected override void onIntentionCast(Skill skill, Creature target)
	    {
		    setAttackTarget(target);
		    changeIntention(CtrlIntention.AI_INTENTION_CAST, skill, target);
		    onEvtThink();
	    }
	    
	    protected override void onIntentionFollow(Creature target, Integer offset)
	    {
		    changeIntention(CtrlIntention.AI_INTENTION_FOLLOW, target, offset);
		    onEvtThink();
	    }
	    
	    protected override void onIntentionInteract(GameObject obj)
	    {
	    }

        public override void onIntentionPickUp(GameObject item)
	    {
	    }
        
	    protected override void onIntentionRest()
	    {
	    }
	    
	    protected override void onIntentionCoupleAction(Player player, Integer socialId)
	    {
	    }
	    
	    protected override void onEvtArrivedBlocked(Location blocked_at_pos)
	    {
		    Creature actor = getActor();
		    if (actor.isPlayer())
		    {
			    Location loc = ((Player) actor).getLastServerPosition();
			    if (loc != null)
			    {
				    actor.setLoc(loc, true);
			    }
			    actor.stopMove();
		    }
		    onEvtThink();
	    }
	    
	    protected override void onEvtForgetObject(GameObject obj)
	    {
		    if (obj== null)
		    {
			    return;
		    }
		    Creature actor = getActor();
		    if (actor.isAttackingNow() && (getAttackTarget() == obj))
		    {
			    actor.abortAttack(true, true);
		    }
		    if (actor.isCastingNow() && (getAttackTarget() == obj))
		    {
			    actor.abortCast(true, true);
		    }
		    if (getAttackTarget() == obj)
		    {
			    setAttackTarget(null);
		    }
		    if (actor.getTargetId() == obj.getObjectId())
		    {
			    actor.setTarget(null);
		    }
		    if (actor.getFollowTarget() == obj)
		    {
			    actor.setFollowTarget(null);
		    }
	    }
        
	    protected override void onEvtDead(Creature killer)
	    {
		    Creature actor = getActor();
		    actor.abortAttack(true, true);
		    actor.abortCast(true, true);
		    actor.stopMove();
		    actor.broadcastPacket(new Die(actor));
		    setIntention(CtrlIntention.AI_INTENTION_IDLE);
	    }
	    
	    protected override void onEvtFakeDeath()
	    {
		    clientStopMoving();
		    setIntention(CtrlIntention.AI_INTENTION_IDLE);
	    }
	    
	    protected override void onEvtAttacked(Creature attacker, int damage)
	    {
	    }
	    
	    protected override void onEvtClanAttacked(Creature attacked_member, Creature attacker, int damage)
	    {
	    }
        public void Attack(GameObject target, bool forceUse, bool dontMove)
	    {
		    setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
	    }
	    public void Cast(Skill skill, Creature target)
	    {
		    Cast(skill, target, false, false);
	    }
	    public virtual void Cast(Skill skill, Creature target, bool forceUse, bool dontMove)
	    {
		    setIntention(CtrlIntention.AI_INTENTION_ATTACK, target);
	    }
	    
	    protected override void onEvtThink()
	    {
	    }
	    
	    protected override void onEvtAggression(Creature target, int aggro)
	    {
	    }
	    
	    protected override void onEvtFinishCasting(int skill_id, bool success)
	    {
	    }
	    
	    protected override void onEvtReadyToAct()
	    {
	    }
        
	    protected override void onEvtArrived()
	    {
	    }
	    
	    protected override void onEvtArrivedTarget()
	    {
	    }
	    
	    protected override void onEvtSeeSpell(Skill skill, Creature caster)
	    {
	    }
	    
	    protected override void onEvtSpawn()
	    {
	    }
	    
	    public override void onEvtDeSpawn()
	    {
	    }
	    public virtual void stopAITask()
	    {
	    }
	    public virtual void startAITask()
	    {
	    }
        public void setNextAction(nextAction action, object arg0, object arg1, bool arg2, bool arg3)
	    {
	    }
	    public virtual void clearNextAction()
	    {
	    }
	    public virtual bool isActive()
	    {
		    return true;
	    }
	    
	    protected override void onEvtTimer(int timerId, object arg1, object arg2)
	    {
	    }
	    
	    protected override void onEvtScriptEvent(String even, object arg1, object arg2)
	    {
	    }
	    
	    protected override void onEvtMenuSelected(Player player, int ask, int reply)
	    {
	    }
	    protected void addTimer(int timerId, long delay)
	    {
		    addTimer(timerId, null, null, delay);
	    }
        protected void addTimer(int timerId, object arg1, long delay)
        {
            addTimer(timerId, arg1, null, delay);
        }
        protected void addTimer(int timerId, object arg1, object arg2, long delay)
        {
            ThreadPoolManager.getInstance().schedule(new Timer(timerId, arg1, arg2), delay);
        }
        protected class Timer:RunnableImpl
        {
            private int _timerId;
		    /**
		     * Field _arg1.
		     */
		    private object _arg1;
		    /**
		     * Field _arg2.
		     */
		    private object _arg2;
		
		    /**
		     * Constructor for Timer.
		     * @param timerId int
		     * @param arg1 Object
		     * @param arg2 Object
		     */
		    public Timer(int timerId, object arg1, object arg2)
		    {
			    _timerId = timerId;
			    _arg1 = arg1;
			    _arg2 = arg2;
		    }
            
		    public override void runImpl()
		    {
			    notifyEvent(CtrlEvent.EVT_TIMER, _timerId, _arg1, _arg2);
		    }
        }
        protected void broadCastScriptEvent(String even, int radius)
	    {
		    broadCastScriptEvent(even, null, null, radius);
	    }
	    protected void broadCastScriptEvent(String even, object arg1, int radius)
	    {
		    broadCastScriptEvent(even, arg1, null, radius);
	    }
	    protected void broadCastScriptEvent(String even, object arg1, object arg2, int radius)
	    {
		    List npcs = World.getAroundNpc(getActor(), radius, radius);
		    foreach (NpcInstance npc in (IEnumerable)npcs)
		    {
			    npc.getAI().notifyEvent(CtrlEvent.EVT_SCRIPT_EVENT, even, arg1, arg2);
		    }
	    }
    }
}
